package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Attr;
import pers.cj.pdos.entity.AttrName;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.service.IAttrNameService;
import pers.cj.pdos.service.IAttrService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.AttrNameVo;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/goods")
public class AttrController extends BaseController {
    @Autowired
    private IAttrService attrService;
    @Autowired
    private IAttrNameService attrNameService;

    @RequiresPermissions("attr:query")
    @RequestMapping("/attr")
    public String attr(){
        return "goods/attr";
    }


    @ResponseBody
    @RequiresPermissions("attr:insert")
    @RequestMapping("/attr/updateAttrName")
    public ResponseEntity updateAttrName(@RequestBody AttrName attrName){
        Company company = getCompany();
        if (attrNameService.checkName(attrName.getName(), company.getId())){
            return new ResponseEntity(2,"属性名已存在");
        }
        DataUtil.processEntity(false,company, attrName);
        attrNameService.update(attrName);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("attr:insert")
    @RequestMapping("/attr/updateAttrValue")
    public ResponseEntity updateAttrValue(@RequestBody Attr attr){
        Company company = getCompany();
        if (attrService.checkAttrValue(attr.getAttrValue(), company.getId())){
            return new ResponseEntity(2,"属性值已存在");
        }

        DataUtil.processEntity(false,company, attr);
        attrService.update(attr);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("attr:insert")
    @RequestMapping("/attr/addAttr")
    public ResponseData addAttr(@RequestBody Attr attr){
        Company company = getCompany();
        if (attrService.checkAttrValue(attr.getAttrValue(), company.getId())){
            return ResponseData.fail("属性值已存在");
        }

        DataUtil.processEntity(company, attr);
        attrService.insert(attr);

        return ResponseData.success(attr);
    }

    @ResponseBody
    @RequiresPermissions("attr:delete")
    @RequestMapping("/attr/deleteAttr")
    public ResponseEntity deleteAttr(@RequestBody AttrNameVo attrNameVo){
        Company company = getCompany();
        try {
            List<Attr> attrList = attrNameVo.getAttrList();
            DataUtil.processList(false,company,attrList);
            attrService.deleteBatch(attrNameVo.getAttrList());
            return ResponseEntity.success();
        } catch (Exception exception) {
            return ResponseEntity.fail("已被使用，删除失败");
        }
    }
    @ResponseBody
    @RequestMapping("/attr/getAll")
    public ResponseData getAll(){
        List<AttrNameVo> attrNameVoList = attrNameService.listVo(getCompany().getId());
        return ResponseData.success(attrNameVoList);
    }

    @ResponseBody
    @RequestMapping("/attr/getByAttrName")
    public ResponseData getByAttrName(String attrNameId){
        List<Attr> list = attrService.listByAttrName(getCompany().getId(), attrNameId);
        return ResponseData.success(list);
    }
}
